package com.itheima.mall.api.ums;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mall.mapper.ums.UmsResourceCategoryMapper;
import com.itheima.mall.pojo.ums.UmsResourceCategory;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class UmsResourceCategoryServiceImpl implements UmsResourceCategoryServiceApi {

    @Autowired
    UmsResourceCategoryMapper umsResourceCategoryMapper;

    @Override
    public R UmsResourceCategoryCreate(UmsResourceCategory umsResourceCategory) {
        umsResourceCategory.setCreateTime(LocalDateTime.now());
        int flag = umsResourceCategoryMapper.insert(umsResourceCategory);

        if (flag>0){
            return R.succeed("","创建成功");
        }

        return R.error("创建失败");
    }

    @Override
    public R UmsResourceCategoryDelete(Long id) {
        int flag = umsResourceCategoryMapper.deleteById(id);
        if (flag>0){
            return R.succeed("","删除成功");
        }

        return R.error("删除失败");
    }

    @Override
    public R UmsResourceCategoryListAll() {
        LambdaQueryWrapper<UmsResourceCategory> lqw = new LambdaQueryWrapper<>();
        List<UmsResourceCategory> umsResourceCategories = umsResourceCategoryMapper.selectList(lqw);
        if (ObjectUtil.isNotNull(umsResourceCategories)){
            return R.succeed(umsResourceCategories,"查询成功");
        }
        return R.error("查询失败");
    }

    @Override
    public R UmsResourceCategoryUpdate(Long id, UmsResourceCategory umsResourceCategory) {
        umsResourceCategory.setId(id);
        int flag = umsResourceCategoryMapper.updateById(umsResourceCategory);
        if (flag>0){
            return R.succeed("","更新成功");
        }

        return R.error("更新失败");    }
}